﻿using Coldairarrow.Entity.BS_Lables;
using Coldairarrow.IBusiness;
using Coldairarrow.IBusiness.BS_Lables;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business
{
    public class BS_LableBusiness : BaseBusiness<BS_Lable>, IBS_LableBusiness, ITransientDependency
    {
        private readonly IOperator _Operator;
        public BS_LableBusiness(IDbAccessor db, IOperator Operators)
            : base(db)
        {
            _Operator = Operators;
        }

        #region 外部接口
        public async Task<PageResult<BS_Lable>> GetDataListAsync(PageInput<TSeachDTO> input)
        {
            input.SortField = "Sort";
            input.SortType = "asc";
            var q = GetIQueryable();

            var where = LinqHelper.True<BS_Lable>();
            var search = input.Search;

            //筛选
            if (!search.Keyword.IsNullOrEmpty())
            {
                where = where.And(x => x.LableName.Contains(search.Keyword));
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<BS_Lable> GetTheDataAsync(string id)
        {

            var tModel = await GetEntityAsync(id);
           
            return tModel;
        }

        public async Task AddDataAsync(BS_Lable data)
        {
            data.LableName = data.LableName.Trim();

            int aCount = GetIQueryable().Where(x => x.LableName.Equals(data.LableName)).Count();
            if (aCount > 0)
            {
                throw new BusException("该标签已存在，请重试！", -3);
            }

            try
            {   
                await InsertAsync(data);
            }
            catch
            {
                throw new BusException("插入失败，请重试！", -2);
            }
        }

        public async Task UpdateDataAsync(BS_Lable data)
        {
            int aCount = GetIQueryable().Where(x => x.LableName.Equals(data.LableName) && x.Id != data.Id).Count();
            if (aCount > 0)
            {
                throw new BusException("该标签名称重复，请重试！", -3);
            }

            try
            {
                await UpdateAsync(data);
            }
            catch
            {
                throw new BusException("修改失败，请重试！", -4);
            }
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            try
            {
                await DeleteAsync(ids);
            }
            catch
            {
                throw new BusException("删除失败，请重试！", -5);
            }
        }

        /// <summary>
        /// 根据类型和模糊检索获取标签
        /// </summary>
        /// <param name="lableName"></param>
        /// <returns></returns>
        public async Task<object> GetLableList(string lableName)
        {

            var dList = await GetIQueryable().WhereIf(!lableName.IsNullOrEmpty(),x => x.LableName.Contains(lableName)).Select(x => new { Val = x.Id, Text = x.LableName }).ToListAsync();

            return dList;
        }

        #endregion 外部接口

        #region 私有成员

        #endregion 私有成员
    }
}